/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javacommunity.controller.convertor;

import com.javacommunity.model.dao.TagDAO;
import com.javacommunity.model.dao.impl.TagDAOImpl;
import com.javacommunity.model.entity.Tag;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Cong Pham Dinh
 */
@FacesConverter("tagConverter")
public class TagConverter implements Converter{
    private final TagDAO tagDAO = TagDAOImpl.getInstance();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        Tag tag = null;
        if (value != null) {
            tag = tagDAO.getTagById(Integer.parseInt(value));
        }
        return tag;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        Tag tag = (Tag) value;
        return tag.getTagId() != null ? String.valueOf(tag.getTagId()) : null;
    }

}
